// pages/user/user/userRecyclingGet/userRecyclingGet.js
const app = getApp();
import Api from '../../../../utils/http';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: '', //袋子编码
    content: '', //领取数量
    userinfo: '', //个人信息
    type: 1,//1为扫一扫 ，2为领取
  },
  //扫一扫
  getScancode: function () {
    var _this = this;
    console.log(_this.data)
    // 允许从相机和相册扫码
    wx.scanCode({
      success: (res) => {
        console.log("扫码结果");
        console.log(res);
        var result = res.result;
        _this.setData({
          result: res.result,
          type: 2
        })
        // _this.getReceiveBag();
      },
      fail: (res) => {
        console.log(res);
      }
    })

  },
  //点击领取
  getReceive: function () {
    this.setData({
      type: 1
    });
    
    this.getReceiveBag();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getuserInfo();
  },

  //个人信息
  getuserInfo: function () {
    var that = this;
    let uid = wx.getStorageSync('userid');
    var param = {
      uid: uid.id,
      user_type: uid.user_type,
    };
    Api.ajax("/app/Zindex/personInfo", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          userinfo: res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  //用户领袋
  getReceiveBag: function () {
    var that = this;
    let uid = wx.getStorageSync('userid')
    var param = {
      user_type: uid.user_type,
      uid: uid.id,
      bag_number: that.data.result.toString(),
      // bag_number:'6914068012254',
    };
    console.log(param)
    Api.ajax("/app/Zcapital/receiveBag", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          content: res
        })
        wx.showModal({
          title: '提示',
          content: '领袋成功',
          confirmText:'确定',
          showCancel:false,
          success(res){
            wx.navigateBack({
              delta:1,
            })
          }
        })
        
      }else if(res.code == 203){
        wx.showModal({
          title: '提示',
          content: '可领取数量不足',
          confirmText:'确定',
          showCancel:false,
        })
      }else if(res.code == 204){
        wx.showModal({
          title: '提示',
          content: '袋子状态有误',
          confirmText:'确定',
          showCancel:false,
        })
      }else if(res.code == 205){
        wx.showToast({
          title: '管理员袋子数量不足',
          duration:1000
        })
      }else{
        wx.showToast({
          title: '袋子领取失败',
          duration:1000
        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})